import { _decorator, Collider, Collider2D, Component, Contact2DType, Node, PolygonCollider2D, Prefab } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('flywheel')
export class flywheel extends Component {
    @property(Prefab)
    flywhell:Prefab = null;
    @property
    lifeTime: number = 2;
    private createTime: number = 0;
    private initScX: number = 1;
    private initScY: number = 1; // 假设初始缩放值为1
    Collider: Collider2D = null;
    hp = 1;
    @property
    damage: number = 10; // flywheel的伤害数值
    start() {
         // 记录当前时间作为创建时间
        this.createTime = Date.now();
        this.Collider = this.node.getComponent(PolygonCollider2D);

        this.Collider.on(Contact2DType.BEGIN_CONTACT, () => {
            this.onTouchEnemy;
          
        }, this);
    }
    onLoad() {
        // 记录初始缩放值
        this.initScX = this.node.scale.x;
        this.initScY = this.node.scale.y;
    }
    update(deltaTime: number) {
        // 每一帧调用一次onFlyWheel方法
        this.onFlyWheel(deltaTime);

        // 检查是否达到生命周期结束
        let elapsedTime = Date.now() - this.createTime;
        if (elapsedTime > this.lifeTime * 1000) {
            this.resetScale(); // 重置缩放值
        }
    }
    onFlyWheel(deltaTime: number) {
        // 获取flywheel的scale
        let scX = this.node.scale.x;
        let scY = this.node.scale.y;
        scX += 0.2 * deltaTime; // 使用deltaTime确保缩放速度不受帧率影响
        scY += 0.2 * deltaTime;
        this.node.setScale(scX, scY);
    }
    resetScale() {
        // 将缩放值重置为初始值
        this.node.setScale(this.initScX, this.initScY);
    }
    onTouchEnemy() {
        // 获取碰撞到的敌人节点
        if(this.hp-=0){
            this.Collider.off(Contact2DType.BEGIN_CONTACT);
        }
    }
    takeDamage(damage: number) {
        // 这个方法可以被敌人调用，以对flywheel造成伤害
        // 这里只是示例，实际游戏中你可能需要更复杂的逻辑
        console.log(`Flywheel takes ${damage} damage`);
    }
    

}


